<?php
/**
 * 管理员权限管理
 */

namespace App\Http\Controllers\Admin;

use App\Http\Requests\PermissionRequest;
use App\Models\Permission;
use Illuminate\Http\Request;

class PermissionController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $menus = $this->_repoService->index($request);
        return $this->display('permission.index', ['menus' => $menus]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //
        $parent_id = $request->input('pid', 0);
        $menus = Permission::submenus(0, false, true);
        //根据pid以此获取pid列表
        $pids = Permission::getParents($parent_id);
        $pids = array_reverse($pids);
        $children = Permission::getChildren(0);
        return $this->display('permission.add', compact('parent_id', 'menus', 'pids', 'children'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(PermissionRequest $request)
    {
        $result = $this->_repoService->store($request->all());
        if ($result['code'] == 1) {
            return $this->success($result['message'], ['redirect' => url('permission')]);
        } else {
            return $this->error($result['message']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request, $id)
    {
        $vo = Permission::find($id);
        $menus = Permission::submenus(0, false, true);
        //根据pid以此获取pid列表
        $pids = Permission::getParents($vo->pid);
        $pids = array_reverse($pids);
        $children = Permission::getChildren(0);
        return $this->display('permission.edit', compact('vo', 'id', 'children', 'pids', 'menus'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(PermissionRequest $request, $id)
    {
        //
        $result = $this->_repoService->update(['id' => $id], $request->all());
        if ($result['code'] == 1) {
            return $this->success($result['message'], ['redirect' => url('permission')]);
        } else {
            return $this->error($result['message']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $result = $this->_repoService->destroy(['id' => $id]);
        if ($result['code'] == 1) {
            return $this->success($result['message'], ['redirect' => url('permission')]);
        } else {
            return $this->error($result['message']);
        }
    }
}
